﻿using Inovout.Cloud.Applications.Models;
using Inovout.Cloud.Applications.Security.Services;
using Inovout.Cloud.Applications.Services;
using Inovout.Cloud.Models;
using Inovout.Cloud.Services;
using Inovout.Cloud.Web.Http;
using Inovout.Cloud.Web.Mvc;
using Inovout.Security;
using Inovout.Services;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Mvc;

namespace Inovout.Cloud.Console.Web.Controllers
{
    public class ProjectController : CloudController<Project>
    {
        private IRepositoryService<Product> productService;
        private IApplicationUserService applicationUserService;
        public ProjectController()
        {
            productService = ComponentRegistry.Resolve<IRepositoryService<Product>>();
            applicationUserService = ComponentRegistry.Resolve<ApplicationUserService>();
        }

        protected IProjectService ProjectService
        {
            get { return (IProjectService)base.RepositoryService; }
        }

        public ActionResult ChangeProduct(int productId, bool enable)
        {
            Product product = productService.FindById(productId);
            ProjectService.ChangeProduct(Project, product, enable);
            return RedirectToAction("My", "Product", new { projectId = base.Project.Id });
        }

        public override ActionResult List(int? pageIndex)
        {
            IList<Project> projects = ProjectService.FindByCurrentMembership();
            return View(projects);
        }

        
        protected override object WrapJsonObect(Project project)
        {
            project.Applications = null;
            project.Membership = null;
            return project;
        }

        public void storage()
        {
            string requestURL = "http://localhost:10200/projct/";
            HttpRequestMessage httpMessage = new HttpRequestMessage(new HttpMethod("get"), requestURL);


            //从permisson中获取privilege不为空的api
            IList<HttpApplicationInterface> listApi = ComponentRegistry.Resolve<HttpApplicationInterfaceService>().FindByPrivilegeNotNull();


            //将api装载到HttpApplicationInterfaceRouteCollection
            HttpApplicationInterfaceRouteCollection httpApiRouteCollection = new HttpApplicationInterfaceRouteCollection(listApi);

            //与当前URL进行匹配，获取url相关的privilege       
            IList<Privilege> listMatchedPriviles = new List<Privilege>();
            IList<HttpApplicationInterface> listMatchedAPIs = httpApiRouteCollection.GetApplicationInterfaces(httpMessage);
            IList<string> listStr = new List<string>();
            foreach (var api in listMatchedAPIs)
            {
                listMatchedPriviles = (IList<Privilege>)listMatchedPriviles.Concat(api.Privileges);
            }
            listMatchedPriviles = (IList<Privilege>)listMatchedPriviles.Distinct().ToList();

            foreach (var privilege in listMatchedPriviles)
            {
                listStr.Add(privilege.ToString());
            }

        }
    }
}